package com.zx.dao.impl;

import com.zx.dao.UserDao;
import com.zx.entity.User;
import com.zx.util.UserUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl extends UserUtils implements UserDao {

    Connection conn=null;
    PreparedStatement pst=null;
    ResultSet rs=null;

    public int regUser(User user){
        conn=getConn();
        int result=0;
        String sql="insert into t_user(user_name,user_pass) values(?,?)";
        try{
            pst=conn.prepareStatement(sql);
            pst.setString(1,user.getName());
            pst.setString(2,user.getPassword());
            result=pst.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeConn(conn,pst,rs);
        }
        return result;
    };
    public User loginUser(User user){
        User u=null;
        try {
            conn=getConn();
            String sql="select * from t_user where user_name = ? and user_pass = ?";
            pst=conn.prepareStatement(sql);
            pst.setString(1,user.getName());
            pst.setString(2,user.getPassword());
            rs= pst.executeQuery();

            if (rs.next()){
                u=new User();
                u.setId(rs.getInt(1));
                u.setName(rs.getString(2));
                u.setPassword(rs.getString(3));
            }


        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeConn(conn,pst,rs);
        }
        return u;
    }
}
